/**
* @author Roy
* @link http://www.ruixinglong.net
* 修复内容为空和多次重复执行bugss
* @version 1.0.1
*/ 
define('limithtml', function(require, exports, module) {
    var $ = require('jquery'),
        def = {
            limit: 30,
            limitheight: 60,
            fill: "...",
            moretext: "展开全部",
            lesstext: "收起",
            cssclass: "limittextclass"
        };

    $.fn.limithtml = function(config) {
        return $(this).each(function(i, e) {
            var $this = $(e),
                opt = $.extend({}, def, config || {}),
                $btn = $this.find('.'+opt.cssclass);
            var html = $this.html();
            if(html && $this.find('.'+opt.cssclass).length < 1){
                var str = html.replace(/<[^>]+>/g,"");
                str = str.replace(/<br\s*\/*\s*>/g,"");

                var getbuttom = function(showtext) {
                    return "<a href='javascript:;' class='" + opt.cssclass + "'>" + showtext + "<a>";
                }

                limiter = function(limit){
                    if ((str.length <= limit || limit == 'all') && str == html || $this.outerHeight() < opt.limitheight) {

                    }
                    else {
                        var btn = getbuttom(opt.moretext);
                        $this.html(str.substr(0, limit).toString() + opt.fill + btn);
                    }
                }
                limiter(opt.limit);

                $this.on('click',$btn.selector,function(){
                    var _posi = $this.offset().top;
                    $('html,body').scrollTop(_posi-100);
                    if ($(this).html() == opt.moretext) {
                        var btn = getbuttom(opt.lesstext);
                        $(this).parent().html(html + "<br>"  + btn);
                    }else{
                        var btn = getbuttom(opt.moretext);
                        $this.html(str.substr(0, opt.limit).toString() + opt.fill + btn);
                    }
                })
            }
            
        })
    }
});